{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas中的数据分组与透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据分组----groupby"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>名称</th>\n",
       "      <th>类别</th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-07-01</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-07-02</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>200</td>\n",
       "      <td>3</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-07-03</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-07-04</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>22</td>\n",
       "      <td>5</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-07-05</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>220</td>\n",
       "      <td>6</td>\n",
       "      <td>1320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2018-07-06</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1000</td>\n",
       "      <td>7</td>\n",
       "      <td>7000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2018-07-07</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2018-07-08</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>800</td>\n",
       "      <td>1</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2018-07-09</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1300</td>\n",
       "      <td>4</td>\n",
       "      <td>5200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2018-07-10</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>230</td>\n",
       "      <td>3</td>\n",
       "      <td>690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2018-07-11</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期   名称  类别    单价  数量    金额\n",
       "0  2018-07-01  商品A  服装    20   2    40\n",
       "1  2018-07-02  商品B  服装   200   3   600\n",
       "2  2018-07-03  商品C  食品  1200   4  4800\n",
       "3  2018-07-04  商品A  服装    22   5   110\n",
       "4  2018-07-05  商品B  服装   220   6  1320\n",
       "5  2018-07-06  商品C  食品  1000   7  7000\n",
       "6  2018-07-07  商品A  服装    30   3    90\n",
       "7  2018-07-08  商品A  服装   800   1   800\n",
       "8  2018-07-09  商品C  食品  1300   4  5200\n",
       "9  2018-07-10  商品B  服装   230   3   690\n",
       "10 2018-07-11  商品A  服装    28   1    28"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel('data.xlsx')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x116c14f60>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped = df.groupby('类别')\n",
    "grouped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>服装</th>\n",
       "      <td>1550</td>\n",
       "      <td>24</td>\n",
       "      <td>3678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <td>3500</td>\n",
       "      <td>15</td>\n",
       "      <td>17000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      单价  数量     金额\n",
       "类别                 \n",
       "服装  1550  24   3678\n",
       "食品  3500  15  17000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>服装</th>\n",
       "      <td>24</td>\n",
       "      <td>3678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <td>15</td>\n",
       "      <td>17000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    数量     金额\n",
       "类别           \n",
       "服装  24   3678\n",
       "食品  15  17000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[['数量', '金额']].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>服装</th>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <td>1300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      单价\n",
       "类别      \n",
       "服装   800\n",
       "食品  1300"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[['单价']].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "grouped = df.groupby(['类别', '名称'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">服装</th>\n",
       "      <th>商品A</th>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品B</th>\n",
       "      <td>230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <th>商品C</th>\n",
       "      <td>1300</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          单价\n",
       "类别 名称       \n",
       "服装 商品A   800\n",
       "   商品B   230\n",
       "食品 商品C  1300"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[['单价']].max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">服装</th>\n",
       "      <th>商品A</th>\n",
       "      <td>180.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品B</th>\n",
       "      <td>216.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <th>商品C</th>\n",
       "      <td>1166.666667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 单价\n",
       "类别 名称              \n",
       "服装 商品A   180.000000\n",
       "   商品B   216.666667\n",
       "食品 商品C  1166.666667"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[['单价']].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "服装\n",
      "           日期   名称  类别   单价  数量    金额\n",
      "0  2018-07-01  商品A  服装   20   2    40\n",
      "1  2018-07-02  商品B  服装  200   3   600\n",
      "3  2018-07-04  商品A  服装   22   5   110\n",
      "4  2018-07-05  商品B  服装  220   6  1320\n",
      "6  2018-07-07  商品A  服装   30   3    90\n",
      "7  2018-07-08  商品A  服装  800   1   800\n",
      "9  2018-07-10  商品B  服装  230   3   690\n",
      "10 2018-07-11  商品A  服装   28   1    28\n",
      "食品\n",
      "          日期   名称  类别    单价  数量    金额\n",
      "2 2018-07-03  商品C  食品  1200   4  4800\n",
      "5 2018-07-06  商品C  食品  1000   7  7000\n",
      "8 2018-07-09  商品C  食品  1300   4  5200\n"
     ]
    }
   ],
   "source": [
    "for name, data in df.groupby('类别'):\n",
    "    print(name)\n",
    "    print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "服装\n",
      "['商品A' '商品B']\n",
      "食品\n",
      "['商品C']\n"
     ]
    }
   ],
   "source": [
    "for name, data in df.groupby('类别'):\n",
    "    print(name)\n",
    "    print(data['名称'].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 透视表----pivot_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>名称</th>\n",
       "      <th>类别</th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-07-01</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>20</td>\n",
       "      <td>2</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-07-02</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>200</td>\n",
       "      <td>3</td>\n",
       "      <td>600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-07-03</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1200</td>\n",
       "      <td>4</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-07-04</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>22</td>\n",
       "      <td>5</td>\n",
       "      <td>110</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-07-05</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>220</td>\n",
       "      <td>6</td>\n",
       "      <td>1320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2018-07-06</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1000</td>\n",
       "      <td>7</td>\n",
       "      <td>7000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2018-07-07</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>30</td>\n",
       "      <td>3</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2018-07-08</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>800</td>\n",
       "      <td>1</td>\n",
       "      <td>800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2018-07-09</td>\n",
       "      <td>商品C</td>\n",
       "      <td>食品</td>\n",
       "      <td>1300</td>\n",
       "      <td>4</td>\n",
       "      <td>5200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2018-07-10</td>\n",
       "      <td>商品B</td>\n",
       "      <td>服装</td>\n",
       "      <td>230</td>\n",
       "      <td>3</td>\n",
       "      <td>690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2018-07-11</td>\n",
       "      <td>商品A</td>\n",
       "      <td>服装</td>\n",
       "      <td>28</td>\n",
       "      <td>1</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期   名称  类别    单价  数量    金额\n",
       "0  2018-07-01  商品A  服装    20   2    40\n",
       "1  2018-07-02  商品B  服装   200   3   600\n",
       "2  2018-07-03  商品C  食品  1200   4  4800\n",
       "3  2018-07-04  商品A  服装    22   5   110\n",
       "4  2018-07-05  商品B  服装   220   6  1320\n",
       "5  2018-07-06  商品C  食品  1000   7  7000\n",
       "6  2018-07-07  商品A  服装    30   3    90\n",
       "7  2018-07-08  商品A  服装   800   1   800\n",
       "8  2018-07-09  商品C  食品  1300   4  5200\n",
       "9  2018-07-10  商品B  服装   230   3   690\n",
       "10 2018-07-11  商品A  服装    28   1    28"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>单价</th>\n",
       "      <th>数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">服装</th>\n",
       "      <th>商品A</th>\n",
       "      <td>180.000000</td>\n",
       "      <td>2.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品B</th>\n",
       "      <td>216.666667</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <th>商品C</th>\n",
       "      <td>1166.666667</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 单价   数量\n",
       "类别 名称                   \n",
       "服装 商品A   180.000000  2.4\n",
       "   商品B   216.666667  4.0\n",
       "食品 商品C  1166.666667  5.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index=['类别', '名称'], values=['单价', '数量'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>数量</th>\n",
       "      <th>金额</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>类别</th>\n",
       "      <th>名称</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">服装</th>\n",
       "      <th>商品A</th>\n",
       "      <td>12</td>\n",
       "      <td>1068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品B</th>\n",
       "      <td>12</td>\n",
       "      <td>2610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>食品</th>\n",
       "      <th>商品C</th>\n",
       "      <td>15</td>\n",
       "      <td>17000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        数量     金额\n",
       "类别 名称            \n",
       "服装 商品A  12   1068\n",
       "   商品B  12   2610\n",
       "食品 商品C  15  17000"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.pivot_table(index=['类别', '名称'], values=['数量', '金额'], aggfunc=np.sum)       # 默认是np.mean  平均"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
